package robot_admin

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"server/src/game/we_chat_group"
	"server/src/global"
	"server/src/model/request"
	"server/src/model/response"
	"server/src/server"
)

type RobotUserAdmin struct {
}

// 微信机器人管理员绑定微信的机器人
func (r *RobotUserAdmin) RobotWeChatLogin(c *gin.Context) {
	//获取用户token
	tokenHeader := c.Request.Header.Get("token")
	userInfo, _ := server.ServerRobotUser.GetUserToRedis(tokenHeader)
	token := userInfo.LoginToken
	//初始化ROBOTS
	if _, ok := we_chat_group.ROBOTS[token]; ok && we_chat_group.ROBOTS[token].Bot != nil {
		we_chat_group.ROBOTS[token].Bot.Logout()
	}
	we_chat_group.ROBOTS[token] = &we_chat_group.RobotInfo{
		LoginiCodeUrl: make(chan string), // 初始化管道
	}
	//获取登录二维码
	go we_chat_group.GetRobotQrcodeUrl(token)
	loginCodeUrl := <-we_chat_group.ROBOTS[token].LoginiCodeUrl
	response.OkWithData(loginCodeUrl, c)
}

// 机器人审核
func (r *RobotUserAdmin) RobotAudit(c *gin.Context) {
	reqInfo := &request.AuditInfo{}
	token := c.Query("token")
	if err := c.ShouldBindJSON(reqInfo); err != nil {
		global.GVL_LOG.Error("接受参数失败", err)
		response.FailWithMessage("查看失败，请重新输入正确的参数", c)
		return
	}
	fmt.Println("RobotAuditUserInfo reqInfo is:", reqInfo)
	//更新数据库   UpAndLower  1 上分  -1 下分
	UpAndLower, GroupName, score, Bool, err := server.ServerRobotUser.UpdateIntegral(reqInfo.GroupUser, reqInfo.AuditBool)
	if err != nil {
		global.GVL_LOG.Error("积分更改失败，请检查用户积分", err)
		response.FailWithMessage("积分更改失败，请检查用户积分"+err.Error(), c)
		return
	} else {
		response.OkWithMessage("积分已更改，请检查用户积分", c)
		if UpAndLower == 1 && Bool {
			we_chat_group.UpwardDivisionSuc(token, GroupName, score)
		} else if UpAndLower == 1 && !Bool {
			we_chat_group.UpwardDivisionFail(token, GroupName, score)
		} else if UpAndLower == -1 && Bool {
			we_chat_group.LowerDivisionSuc(token, GroupName, score)
		} else if UpAndLower == -1 && !Bool {
			we_chat_group.LowerDivisionFail(token, GroupName, score)
		}
	}

}

// 是否发送给系统
func (r *RobotUserAdmin) SendSystem(c *gin.Context) {
	reqInfo := &request.SendSystem{}
	if err := c.ShouldBindJSON(reqInfo); err != nil {
		global.GVL_LOG.Error("接受参数失败", err)
		response.FailWithMessage("查看失败，请重新输入正确的参数", c)
		return
	}
	fmt.Println("SendingSystemUserInfo reqInfo is:", reqInfo)
	//更新数据库
	err := server.ServerRobotUser.UpDataSendSystem(reqInfo.SendingUser, reqInfo.SendingSystemBool)
	if err != nil {
		global.GVL_LOG.Error("操作失败", err)
		response.FailWithMessage("操作失败"+err.Error(), c)
		return
	}
	response.OkWithMessage("操作成功", c)

}

// 设置抽水值
func (r *RobotUserAdmin) MakePuPm(c *gin.Context) {
	reqInfo := &request.PuPm{}
	if err := c.ShouldBindJSON(reqInfo); err != nil {
		global.GVL_LOG.Error("接受参数失败", err)
		response.FailWithMessage("查看失败，请重新输入正确的参数", c)
		return
	}
	fmt.Println("GetPuPmInfo PuPm is:", reqInfo)
	token := c.Request.Header.Get("token")
	userInfo, _ := server.ServerRobotUser.GetUserToRedis(token)
	if userInfo.Id != -1 {
		//数据库中更新群管理员
		err, PuPm := server.ServerRobotUser.UpDataPuPm(reqInfo.PuPm, userInfo.Id)
		if err != nil {
			global.GVL_LOG.Error("抽水更新失败", err)
			response.FailWithMessage("抽水更新失败", c)
			return
		} else {
			//抽水信息进行更新到cache中
			if _, ok := we_chat_group.ROBOTS[userInfo.LoginToken]; ok && we_chat_group.ROBOTS[userInfo.LoginToken].GroupInfo != nil {
				we_chat_group.ROBOTS[userInfo.LoginToken].GroupInfo.Pump = PuPm
			}
			Ppm := make(map[string]float64, 1)
			Ppm["抽水值"] = PuPm
			response.OkWithDetailed(Ppm, "抽水更新成功", c)
		}
	}
}

// 获取抽水值
func (r *RobotUserAdmin) GetPuPm(c *gin.Context) {
	//获取机器管理序号
	token := c.Request.Header.Get("token")
	userInfo, _ := server.ServerRobotUser.GetUserToRedis(token)
	err, PuPm := server.ServerRobotUser.GetPuPm(userInfo.Id)
	if err != nil {
		global.GVL_LOG.Error("获取抽水值失败", err)
		response.FailWithMessage("获取抽水值更新失败", c)
		return
	}
	Ppm := make(map[string]float64, 1)
	Ppm["PuPm"] = PuPm
	response.OkWithDetailed(Ppm, "获取抽水值成功", c)
}
